Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8.x] Add forwardDecoratedCallTo #38800

Merged
merged 3 commits into from
Sep 13, 2021

Conversation

inxilpro
Copy link
Contributor

This PR adds an additional method to ForwardsCalls to allow for easy fluent calls to decorated objects. This pattern is used in the Relation class as well as in the DecoratesQueryBuilder trait that will be introduced in Laravel 9. Under the hood, this essentially performs:

public function __call($method, $parameters)
{
  $result = $this->forwardCallTo($this->decorated, $method, $parameters);

  // If the underlying call was fluent, make this call fluent as well
  if ($result === $this->decorated) {
    return $this;
  }

  return $result;
}

This is a nice solution, because we continue to receive our proxy object until we make a call that isn't fluent on the underlying decorated object.

There are a handful of places that could be updated to use this new method. Because these are mild breaking changes, I'll introduce them in 9.x PR should this PR get merged. These are:

  • Illuminate\Events\NullDispatcher
  • Illuminate\Http\Resources\DelegatesToResource
  • Illuminate\Mail\Message
  • Illuminate\Pagination\AbstractPaginator and AbstractCursorPaginator

In each of these cases, I believe the expected behavior of any delegated fluent call would be to receive the proxy object, whereas right now you would receive the underlying object.

For example, in the case of Illuminate\Mail\Message:

// Works because you happen to call from() first
$message->from('foo@bar.com')->addPart('hello', 'text/plain');

// Fails because you call from() second, and `addPart()` returned the underlying Swift_Message
$message->addPart('hello', 'text/plain')->from('foo@bar.com');

@taylorotwell taylorotwell merged commit a84a2ed into laravel:8.x Sep 13, 2021
chu121su12 pushed a commit to chu121su12/framework that referenced this pull request Sep 14, 2021
* [8.x] Add forwardDecoratedCallTo

* Code style

* Update ForwardsCalls.php

Co-authored-by: Taylor Otwell <taylor@laravel.com>
wouterj pushed a commit to wouterj/laravel-framework that referenced this pull request Sep 15, 2021
* [8.x] Add forwardDecoratedCallTo

* Code style

* Update ForwardsCalls.php

Co-authored-by: Taylor Otwell <taylor@laravel.com>
victorvilella pushed a commit to cdsistemas/framework that referenced this pull request Oct 12, 2021
* [8.x] Add forwardDecoratedCallTo

* Code style

* Update ForwardsCalls.php

Co-authored-by: Taylor Otwell <taylor@laravel.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants